import type { AxiosInstance } from 'axios';
import { apiAxios } from './build/apis';

import { msgHandler } from './build/index';

const { VITE_APIS_WEB } = import.meta.env;

export const FORM_HEADER =  {
  'Content-Type': 'multipart/form-data;charset=UTF-8',
};

//登录请求
export const loginAxios:AxiosInstance = apiAxios({ baseURL:`${VITE_APIS_WEB}` });

class newAxios {
  baseURL: string;
  constructor() {
    this.baseURL = `${VITE_APIS_WEB}/p`;
  }
  async get(url:string, response={}, config={}) { 
    return new Promise((resolve,reject)=>{
			apiAxios({ baseURL: this.baseURL }).get(url, response).then((result:Recordable) => {

        const success = result?.ecode === 0 || (result?.status && result?.status == 200); // 成功状态

        const data = msgHandler(success, config?.showMsg || 0, result?.msg, 'GET', result);

        resolve(data);
      }).catch((err) => reject(err));
		});
  }

  async post(url:string, response={}, config={}) { 
    return new Promise((resolve,reject)=>{
			apiAxios({ baseURL: this.baseURL }).post(url, response).then((result: Recordable)=> {

        const success = result?.ecode === 0; // 成功状态

        const data = msgHandler(success, config?.showMsg || 0, result?.msg, 'POST', result);

        resolve(data);
      }).catch((err) => reject(err));
		});
  }

  async postForm(url:string, response={}, config={}) { 
    return new Promise((resolve,reject)=>{
			apiAxios({ baseURL: this.baseURL }).postForm(url, response).then((result: Recordable)=> {

        const success = result?.ecode === 0; // 成功状态

        const data = msgHandler(success, config?.showMsg || 0, result?.msg, 'POST', result);

        resolve(data);
      }).catch((err) => reject(err));
		});
  }
}

// 请求
export const reqAxios = new newAxios();
